package com.neusoft.nep.SystemAdminstrator.Controller;

import com.neusoft.nep.SystemAdminstrator.Common.FileUtills;
import com.neusoft.nep.SystemAdminstrator.entity.ProvFeedback;
import com.neusoft.nep.entity.GirdFeedback;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProvincialGroupComponentItemInspectionStatisticsController {
    @FXML
    private TableColumn<ProvFeedback, Integer> index;
    @FXML
    private TableColumn<ProvFeedback, String> ProvSimple;
    @FXML
    private TableColumn<ProvFeedback, String> Province;
    @FXML
    private TableColumn<ProvFeedback, Integer> SO2;
    @FXML
    private TableColumn<ProvFeedback, Integer> CO;
    @FXML
    private TableColumn<ProvFeedback, Integer> PM25;
    @FXML
    private TableColumn<ProvFeedback, Integer> AQIlevel;
    @FXML
    private TableView<ProvFeedback> tableView;
    private ObservableList<ProvFeedback> tablelist = FXCollections.observableArrayList();

    @FXML
    private Button btn_out;
    @FXML
    private Label labID;
    @FXML
    private Button btn_back;
    @FXML
    private MenuItem mi_PSL;
    @FXML
    private MenuItem mi_AQI;
    @FXML
    private MenuItem mi_PGC;
    @FXML
    private MenuItem mi_AqiDistribute;
    @FXML
    private MenuItem mi_Other;
    @FXML
    private MenuItem mi_AqiTrendTotal;

    // 省份简称映射表
    private static final Map<String, String> PROVINCE_ABBREVIATIONS = new HashMap<>();
    static {
        PROVINCE_ABBREVIATIONS.put("辽宁省", "辽");
        PROVINCE_ABBREVIATIONS.put("吉林省", "吉");
        PROVINCE_ABBREVIATIONS.put("黑龙江省", "黑");
        // 可根据需要添加更多省份映射
    }

    public void initialize() {
        labID.setText("系统管理员：" + loginController.currentLoginID);
        index.setCellValueFactory(new PropertyValueFactory<>("ProvIndex"));
        ProvSimple.setCellValueFactory(new PropertyValueFactory<>("ProvSimple"));
        Province.setCellValueFactory(new PropertyValueFactory<>("province"));
        SO2.setCellValueFactory(new PropertyValueFactory<>("SO2num"));
        CO.setCellValueFactory(new PropertyValueFactory<>("COnum"));
        PM25.setCellValueFactory(new PropertyValueFactory<>("PM25num"));
        AQIlevel.setCellValueFactory(new PropertyValueFactory<>("AQIlevelnum"));
        int i = 0;

        List<String> list = FileUtills.readListObject("AssignofficerNames.txt");

            for (String string : list) {
                i++;
                Map<String, GirdFeedback> girdFeedbackMap = FileUtills.readMapObject("gird" + string + "back.txt");
                for (GirdFeedback girdFeedback : girdFeedbackMap.values()) {
                    girdFeedback.setGridName(string);

                    // 创建新的ProvFeedback对象并手动赋值
                    ProvFeedback provFeedback = convertToProvFeedback(girdFeedback);
                    provFeedback.setProvIndex(i);

                    // 统计超标数量
                    provFeedback.setSO2num(countOverlimit(girdFeedback.getSO2()));
                    provFeedback.setCOnum(countOverlimit(girdFeedback.getCO()));
                    provFeedback.setPM25num(countOverlimit(girdFeedback.getPM()));
                    provFeedback.setAQIlevelnum(countOverlimit(girdFeedback.getAqiLevel()));

                    tablelist.add(provFeedback);
                }
            }
            tableView.setItems(tablelist);


    }

    // 将GirdFeedback转换为ProvFeedback的方法
    private ProvFeedback convertToProvFeedback(GirdFeedback girdFeedback) {
        ProvFeedback provFeedback = new ProvFeedback(
                girdFeedback.getIndex(),
                girdFeedback.getName(),
                girdFeedback.getSex(),
                girdFeedback.getPhoneNumber(),
                girdFeedback.getBirthday(),
                girdFeedback.getProvince(),
                girdFeedback.getCity(),
                girdFeedback.getAddress(),
                girdFeedback.getAqiLevel(),
                girdFeedback.getDescription(),
                girdFeedback.getTime(),
                girdFeedback.getSO2(),
                girdFeedback.getCO(),
                girdFeedback.getPM(),
                girdFeedback.getLevel(),
                girdFeedback.getLevelInfo()
        );

        // 设置ProvFeedback特有的属性
        provFeedback.setProvSimple(getProvinceAbbreviation(girdFeedback.getProvince()));
        return provFeedback;
    }

    // 获取省份简称的方法
    private String getProvinceAbbreviation(String province) {
        if (province == null) return "";
        return PROVINCE_ABBREVIATIONS.getOrDefault(province, province);
    }

    // 统计超标数量的方法
    private int countOverlimit(String level) {
        if (level == null) return 0;
        // 假设等级为"一"到"六"的字符串，"三"及以上为超标
        switch (level) {
            case "三":
            case "四":
            case "五":
            case "六":
                return 1;
            default:
                return 0;
        }
    }

    public void OUT(ActionEvent actionEvent) {
        System.exit(0);
    }

    public void toback(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/index.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_back.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    public void PSLClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/publicList.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    public void AQIListClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AQImakesure.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    public void PGCClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/ProvincialGroupComponentItemInspectionStatistics.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    public void AqiDistributeClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AqiDistributeTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    public void OtherClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/OtherTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    public void AqiTrendClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AqiTrendTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
}